package org.example.l0;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.PriorityQueue;

public class longestConsecutive {

    @Test
    void aabb() {
        longestConsecutive(new int[]{9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6});
    }

    public int longestConsecutive(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }

        PriorityQueue<Integer> queue = new PriorityQueue<>();
        Arrays.stream(nums).distinct().forEach(queue::offer);

        int max = Integer.MIN_VALUE, former = queue.poll();
        int cnt = 1;
        while (!queue.isEmpty()) {
            Integer v = queue.poll();
            if (v - former == 1) {
                cnt++;
            } else {
                max = Math.max(cnt, max);
                cnt = 1;
            }
            former = v;
        }
        max = Math.max(cnt, max);

        return max;
    }
}
